package com.molyfun.parents.fragment.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.utils.Const;

public class RefundDetailDialogFragment extends DialogFragment implements View.OnClickListener{

    private static RefundDetailDialogFragment newInstance(String title, String content) {
        RefundDetailDialogFragment mStudentMenuDialogFragment = new RefundDetailDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Const.FLAG_REFUND_AMOUNT, title);
        bundle.putSerializable(Const.FLAG_REFUND_REASON, content);
        mStudentMenuDialogFragment.setArguments(bundle);
        return mStudentMenuDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setStyle(DialogFragment.STYLE_NORMAL, R.style.BaseDialog);
    }

    @Override public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (null != dialog) {
            dialog.getWindow().setLayout(-1, -2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().setCanceledOnTouchOutside(false);
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);

        View view = inflater.inflate(R.layout.dialog_refund_detail, container, false);
        String refundAmount = (String)getArguments().get(Const.FLAG_REFUND_AMOUNT);
        String refundReason = (String)getArguments().get(Const.FLAG_REFUND_REASON);

        ((TextView)view.findViewById(R.id.refund_amount)).setText(refundAmount + "元");
        ((TextView)view.findViewById(R.id.refund_reason)).setText(refundReason);
        view.findViewById(R.id.close).setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                break;
            default:
                break;
        }
        getDialog().dismiss();
    }

    public static class RefundDetailDialogBuilder {

        RefundDetailDialogFragment mCommonDialogFragment;
        private String mTag;

        public RefundDetailDialogBuilder(String amount, String reason){
            mCommonDialogFragment = RefundDetailDialogFragment.newInstance(amount, reason);
            mTag = "refund_detail";
        }

        public RefundDetailDialogBuilder setTag(String tag){
            this.mTag = tag;
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction){
            fragmentTransaction.add(mCommonDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }

    }
}
